package com.hurt.competition.judge.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hurt.competition.admin.dto.CompetitionReq;
import com.hurt.competition.admin.model.Competition;
import com.hurt.competition.common.util.CoreResultBuilder;
import com.hurt.competition.common.vo.CoreResult;
import com.hurt.competition.common.vo.PageResult;
import com.hurt.competition.judge.dto.CompetitionJudgeListRes;
import com.hurt.competition.judge.dto.CompetitionJudgeReq;
import com.hurt.competition.judge.model.CompetitionJudge;
import com.hurt.competition.judge.service.CompetitionJudgeService;
import com.hurt.competition.player.dto.CompetitionPlayerListRes;
import com.hurt.competition.player.dto.CompetitionPlayerReq;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("judge")
@Slf4j
public class CompetitionJudgeController {

    @Autowired
    private CompetitionJudgeService competitionJudgeService;

    @PostMapping("page")
    private CoreResult page(@RequestBody CompetitionJudgeReq req){
        PageResult<Competition> page = competitionJudgeService.page(req);
        return CoreResultBuilder.success(page);
    }

    // 查询该比赛参加的评委
    @PostMapping("list")
    private CoreResult list(@RequestBody CompetitionJudgeReq req){
        PageResult<CompetitionJudgeListRes> page = competitionJudgeService.queryList(req);
        return CoreResultBuilder.success(page);
    }

    @PostMapping("apply")
    public CoreResult apply(@RequestBody CompetitionJudge competitionJudge){
        log.info("申请成为比赛评委");
        if(competitionJudge.getCompetitionId() == null || competitionJudge.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }
        competitionJudgeService.apply(competitionJudge);
        return CoreResultBuilder.success("申请成功");
    }

    @PostMapping("audit")
    public CoreResult audit(@RequestBody CompetitionJudge competitionJudge){
        log.info("审批申请");
        if(competitionJudge.getCompetitionId() == null || competitionJudge.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }
        competitionJudgeService.audit(competitionJudge);
        return CoreResultBuilder.success("成功");
    }

    @PostMapping("delete")
    public CoreResult delete(@RequestBody CompetitionJudge competitionJudge){
        log.info("删除申请");
        if(competitionJudge.getCompetitionId() == null || competitionJudge.getUserId() == null){
            return CoreResultBuilder.error("参数不能为空");
        }

        LambdaQueryWrapper<CompetitionJudge> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CompetitionJudge::getCompetitionId,competitionJudge.getCompetitionId())
                .eq(CompetitionJudge::getUserId,competitionJudge.getUserId());
        competitionJudgeService.remove(queryWrapper);
        return CoreResultBuilder.success("成功");
    }
}
